export const http = (url, options = {}) => {
	uni.showLoading()
	const baseUrl = 'https://tea.qingnian8.com/api/bizhi/'
	if (!url.startsWith('http')) {
		url = baseUrl + (url.startsWith('/') ? url.slice(1) : url)
	}
	const token = uni.getStorageSync('token')
	const options_deflut = {
		url,
		method: 'GET',
		timeout: 5000,
		header: {
			'access-key': token
		},
		...options
	}
	
	return new Promise((reslove, reject) => {
		uni.request({
			...options_deflut,
			success: res => {
				if (res.data.errCode === 0) {
					reslove(res.data)
				} else if (res.data.errCode === 400) {
					uni.showModal({
						title: '错误提示',
					  content: res.data.errMsg || '请求失败',
						showCancel:false
					})
					reject(res.data.errMsg)
				} else {
					uni.showToast({
						title: res.data.errMsg || res.data.error?.message || '请求失败else',
						icon: 'none'
					})
					reject(res.data.errMsg || res.data.error?.message || '请求失败else')
				}
			},
			fail: reason => {
				uni.showToast({
					title: reason.data.errMsg || '请求失败',
					icon: 'none'
				})
				reject(reason.data.errMsg)
			},
			complete: () => {
				uni.hideLoading()
				uni.stopPullDownRefresh()
			}
		})
	})
}